package com.example.demo.controller;

import com.example.demo.domain.Announcement;
import com.example.demo.domain.User;
import com.example.demo.service.AnnouncementService;
import com.example.demo.util.ParseUsername;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: demo
 * @description: 公告的控制器
 * @author: wangze
 * @create: 2020-05-21 20:28
 **/
@RestController
@RequestMapping
@CrossOrigin
@Slf4j
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    private List<Announcement> announcementList;

    /**
     * @Description:  获取对应公告列表
     * @Param:  nowPage：当前页码
     * @Param: pageSize：页码大小
     * @Param: searchContent：搜索内容
     * @return: 公告列表
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('announcement','interaction:announcement:list')")
    @GetMapping(value = "/announcements")
    public String getAnnouncements(@RequestParam("nowPage")Integer nowPage,
                                   @RequestParam("pageSize")Integer pageSize,
                                   @RequestParam("searchContent")String searchContent) {
//        System.out.println("获取公告列表");
        if(searchContent==""){
            announcementList = announcementService.getAllAnnouncement();
        }else{
            announcementList = announcementService.searchAnnouncement(searchContent);
        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total",announcementList.size());
        List<Announcement> announcements = announcementList.subList((nowPage - 1) * pageSize,Math.min( nowPage * pageSize,announcementList.size()));
        JSONArray jsonArray = new JSONArray();
        for(Announcement temp:announcements){
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("id",temp.getId());
            jsonObject1.put("introduction",temp.getIntroduction());
            jsonObject1.put("content",temp.getContent());
            Date date = temp.getDate();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String time = dateFormat.format(date);
            jsonObject1.put("date",time);
            jsonArray.add(jsonObject1);
        }
        jsonObject.put("announcements",jsonArray);
        return  jsonObject.toString();
    }

    @PreAuthorize("hasPermission('users','users:permissions:basic')")
    @GetMapping(value = "/wx-announcements")
    public List<Announcement> getAllAnnouncements(){
        return announcementService.getAllAnnouncement();
    }

    /**
    * @Description:  添加公告
    * @Param:  announcement: 公告实体
    * @return:  公告实体
    * @Author: wangze
    */
    @PreAuthorize("hasPermission('announcement','interaction:announcement:add')")
    @PostMapping(value = "/announcement")
    public Announcement addAnnouncement(@RequestBody Announcement announcement){
//        System.out.println("添加公告");
        announcement.setDate(new Date());
        log.info("管理员"+ ParseUsername.getUsername()+"添加了一条公告，公告标题:"+announcement.getIntroduction()+"  公告内容:"+announcement.getContent());
        return announcementService.addAnnouncement(announcement);
    }

    /**
     * @Description:  删除公告
     * @Param:  id: 公告ID
     * @return:  无
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('announcement','interaction:announcement:delete')")
    @DeleteMapping(value = "/announcement")
    public void deleteAnnouncement(@RequestParam("announcementId")Integer id){
        Announcement announcement=announcementService.getAnnouncement(id);
        log.info("管理员"+ ParseUsername.getUsername()+"删除了一条公告，公告标题:"+announcement.getIntroduction()+"  公告内容:"+announcement.getContent());
        announcementService.deleteAnnouncement(id);
    }

    /**
     * @Description:  更新公告
     * @Param:  announcement: 公告实体
     * @return:  公告实体
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('announcement','interaction:announcement:update')")
    @PutMapping(value = "/announcement")
    public Announcement updateAnnouncement(@RequestBody Announcement announcement){
        log.info("管理员"+ ParseUsername.getUsername()+"更新了一条公告，公告标题:"+announcement.getIntroduction()+"  公告内容:"+announcement.getContent());
        return announcementService.updateAnnouncement(announcement);
    }

    /**
     * @Description:  查看公告
     * @Param:  id: 公告ID
     * @return:  公告实体
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('users','users:permissions:basic')")
    @GetMapping(value = "/announcement")
    public Announcement getAnnouncement(@RequestParam("announcementId")Integer id){
        return announcementService.getAnnouncement(id);
    }

    @GetMapping(value = "/test-announcement")
    public String testAnnouncement(){
        return "Announcement中的测试方法";
    }
}
